// Copyright 2010 Google Inc. All Rights Reserved.

/**
 * @fileoverview Helper functions.
 *
 * @author monsur@google.com (Monsur Hossain)
 */

goog.provide('googleapis');
goog.provide('googleapis.server');
goog.provide('googleapis.session');


/**
 * @param {string} str The source string.
 * @param {string} start The string to search for.
 * @return {boolean} Whether the source string starts with the search string.
 */
googleapis.startsWith = function(str, start) {
  return (str.indexOf(start) === 0);
};


/**
 * @param {string} str The source string.
 * @param {string} end The string to search for.
 * @return {boolean} Whether the source string ends with the search string.
 */
googleapis.endsWith = function(str, end) {
  var lastIndex = str.lastIndexOf(end);
  return (lastIndex != -1) && (lastIndex + end.length == str.length);
};


/**
 * @param {string} str The source string.
 * @param {Array.<string>} endArr The strings to search for.
 * @return {boolean} Whether the source string ends with any of the strings in
 *     the array.
 */
googleapis.endsWithAny = function(str, endArr) {
  for (var i = 0, len = endArr.length; i < len; i++) {
    if (googleapis.endsWith(str, endArr[i])) {
      return true;
    }
  }
  return false;
};


/**
 * @param {string} method The full RPC method name (for example,
 *     "buzz.activities.search").
 * @return {string} The service name (for example, "buzz").
 */
googleapis.getServiceNameFromMethod = function(method) {
  return method.substring(0, method.indexOf('.'));
};


/**
 * Initialize.
 */
googleapis.init = function() {
  googleapis.ApiClient.init();
};


/**
 * @param {string} method The RPC method name.
 */
googleapis.register = function(method) {
  googleapis.ApiClient.register(method);
};


/**
 * @param {Object} params The config parameters.
 * @param {boolean=} opt_sticky Whether to persist the config in the cookie.
 */
googleapis.configure = function(params, opt_sticky) {
  googleapis.Config.configure(params, opt_sticky);
};


/**
 * Clear config.
 */
googleapis.clearconfig = function() {
  googleapis.Config.clearConfig();
};


/**
 * @param {string} feature This is todo.
 * @return {Object} This is todo.
 */
googleapis.getFeatureConfig = function(feature) {
  return googleapis.Config.getFeatureConfig(feature);
};


/**
 * Makes an RPC request against an arbitrary method.  Mimics the interface of
 * the "vertical" api calls (such as "chili.activities.search"), but includes
 * and additional "method" parameter that indicates which method to call.
 * @param {string} method The method name.
 * @param {Object} params The parameters to the method.
 * @this *
 * @return {{execute: function(Function)}} The request object
 *     (use .execute(callback) to make the actual request).
 */
googleapis.newRequest = function(method, params) {
  // Note that the logic here is a subset of
  // what is found in osapi._registerMethod.
  params = params || {};
  var transport = {
      'name': 'googleapis',
      'execute': googleapis.ApiClient.execute_};
  var batch = osapi.newBatch();

  var that = this;

  var boundCall = {};
  boundCall['method'] = method;
  boundCall['transport'] = transport;
  boundCall['rpc'] = params;
  boundCall['execute'] = function(callback) {
    batch.add(method, {'method': method, 'rpc': params,
        'transport': transport});
    batch.execute(function(batchResult) {
      if (batchResult['error']) {
        callback.call(that, batchResult['error']);
      } else {
        callback.call(that, batchResult[method]);
      }
    });
  }

  return boundCall;
};


/**
 * @return {googleapis.Batch} A new batch object.
 */
googleapis.newBatch = function() {
  return new googleapis.Batch();
};


/**
 * @return {googleapis.Poll} A new poll object.
 */
googleapis.newPoll = function() {
  return new googleapis.Poll();
};


/**
 * Set the versions to use for each API.
 * @param {Object} obj A mapping of service name to version, for example:
 *     {'chili': 'v1'}.
 */
googleapis.setVersions = function(obj) {
  googleapis.ApiClient.setVersions(obj);
}


/**
 * Initialize the XD3 proxy.
 */
googleapis.server.init = function() {
  // Register makeRequest
  gadgets.rpc.register('makeRequest', function(requestObj) {
    var callback = this['callback'];
    googleapis.ApiServer.makeRequest(requestObj, callback);
  });

  gadgets.rpc.call('..', 'ready');
};
